package cn.kgc.controller;

import cn.kgc.entity.User;
import cn.kgc.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rock
 * @create 2020-06-17 16:51
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(
            value="添加用户",
            notes = "添加",
            httpMethod = "POST",
            produces = "application/json",
            protocols = "HTTP",
            response = User.class)
    @PostMapping("add")
    private User addUser(@ApiParam(name = "name",value = "用户名称")String name,@ApiParam(name = "age",value = "用户年龄")Integer age){
        User user = new User();
        user.setName(name);
        user.setAge(age);
        userService.saveUser(user);
        return user;
    }
    @DeleteMapping("del")
    private String delUser(Integer id){
        userService.deleteUser(id);
        return "success";
    }
    @ApiOperation(
            value="根据id修改用户姓名",
            notes = "修改",
            httpMethod = "PUT",
            produces = "application/json",
            protocols = "HTTP",
            response = User.class)
    @PutMapping("update/{id}/{name}")
    private User updateUser(@ApiParam(name = "id",value = "用户id") @PathVariable("id") Integer id,@ApiParam(name = "name",value = "用户姓名") @PathVariable("name")String name){
        User user = userService.findUser(id);
        user.setName(name);
        userService.updateUser(user);
        return user;
    }

    @ApiOperation(
            value="根据id查询用户",
            notes = "id查询",
            httpMethod = "GET",
            produces = "application/json",
            protocols = "HTTP",
            response = User.class)
    @GetMapping("find/{id}")
    private User findUser(@ApiParam(name = "id",value="用户id",required = true) @PathVariable("id") Integer id){
        return userService.findUser(id);
    }

    @ApiOperation(
            value="查询所有用户",
            notes = "查询所有",
            httpMethod = "GET",
            produces = "application/json",
            protocols = "HTTP",
            response = User.class)
    @GetMapping("findAll")
    private List<User> findAllUser(){
        return userService.findAll();
    }
}
